package com.zcp.service;


import com.zcp.dao.UserDAO;
import com.zcp.entity.User;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service
@Transactional
public class UserServiceImpl implements UserService{
    @Autowired
    private UserDAO userDAO;

    @Override
    public void register(User user) {
        User user1 = userDAO.findname(user.getName());
        if (user1!=null){
            throw new RuntimeException("当前用户名已被注册，请修改后再试");
        }
        user.setRegtime(new Date());
        user.setStatus("已激活");
        userDAO.save(user);
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS)
    public User login(User user) {
        User user1 = userDAO.findname(user.getName());
        if(user1!=null){
            if (StringUtils.equals(user1.getPassword(),user.getPassword())){
                return user1;
            }else {
                throw new RuntimeException("密码错误");
            }
        }else {
            throw new RuntimeException("用户名不存在");
        }
    }
}
